from django.db import models
from users.models import User

class Building(models.Model):
    building_id = models.BigAutoField(primary_key=True, db_comment='楼宇ID')
    park = models.ForeignKey('assets.Park', models.DO_NOTHING, db_comment='园区ID')
    name = models.CharField(max_length=50, db_comment='名称/编号')
    total_floors = models.IntegerField(db_comment='总层数')
    building_area = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='建筑面积(平方米)')
    completion_date = models.DateField(blank=True, null=True, db_comment='建成日期')
    building_type = models.CharField(max_length=20, db_comment='类型(办公楼/厂房/研发楼)')
    structure_type = models.CharField(max_length=50, blank=True, null=True, db_comment='结构类型')
    fire_rating = models.CharField(max_length=20, blank=True, null=True, db_comment='消防等级')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'building'
        db_table_comment = '楼宇表'

class Floor(models.Model):
    floor_id = models.BigAutoField(primary_key=True, db_comment='楼层ID')
    building = models.ForeignKey(Building, models.DO_NOTHING, db_comment='楼宇ID')
    floor_number = models.IntegerField(db_comment='楼层号')
    actual_label = models.CharField(max_length=10, db_comment='实际标号(如G/F1/M)')
    height = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, db_comment='层高(米)')
    load_bearing = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='承重(kg/㎡)')
    function_zone_desc = models.TextField(blank=True, null=True, db_comment='功能分区描述')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'floor'
        unique_together = (('building', 'floor_number'),)
        db_table_comment = '楼层表'

class Park(models.Model):
    park_id = models.BigAutoField(primary_key=True, db_comment='园区ID')
    name = models.CharField(max_length=100, db_comment='名称')
    address = models.TextField(db_comment='地址')
    description = models.TextField(blank=True, null=True, db_comment='描述')
    establishment_date = models.DateField(db_comment='成立日期')
    total_area = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='总面积(平方米)')
    contact_info = models.TextField(blank=True, null=True, db_comment='联系方式')
    logo_url = models.CharField(max_length=255, blank=True, null=True, db_comment='Logo URL')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'park'
        db_table_comment = '园区表'

class Unit(models.Model):
    unit_id = models.BigAutoField(primary_key=True, db_comment='单元ID')
    floor = models.ForeignKey(Floor, models.DO_NOTHING, blank=True, null=True, db_comment='楼层ID')
    building = models.ForeignKey(Building, models.DO_NOTHING, db_comment='楼宇ID')
    unit_number = models.CharField(max_length=50, db_comment='单元编号')
    actual_area = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='实际面积(平方米)')
    building_area = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='建筑面积(平方米)')
    unit_type = models.CharField(max_length=20, db_comment='类型(办公室/商铺/车位/仓库/会议室/广告位)')
    status = models.CharField(max_length=20, db_comment='当前状态(空置/意向/已租/已售/装修中/维修中/待清洁/不可用)')
    rental_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='租金单价')
    sale_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='售价')
    property_fee_standard = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='物业费标准')
    orientation = models.CharField(max_length=20, blank=True, null=True, db_comment='朝向')
    facilities_desc = models.TextField(blank=True, null=True, db_comment='配套设施描述')
    media_urls = models.TextField(blank=True, null=True, db_comment='图片/视频资料URL列表(JSON)')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'unit'
        db_table_comment = '单元/房源表'

class Device(models.Model):
    device_id = models.BigAutoField(primary_key=True, db_comment='设备ID')
    building = models.ForeignKey(Building, models.DO_NOTHING, db_comment='所属楼宇ID')
    floor = models.ForeignKey(Floor, models.DO_NOTHING, blank=True, null=True, db_comment='所属楼层ID (可选)')
    unit = models.ForeignKey(Unit, models.DO_NOTHING, blank=True, null=True, db_comment='所属单元ID (可选)')
    device_name = models.CharField(max_length=100, db_comment='设备名称')
    device_number = models.CharField(max_length=50, db_comment='设备编号')
    device_type = models.CharField(max_length=50, db_comment='设备类型(空调/电梯/消防栓/门禁)')
    location_desc = models.TextField(blank=True, null=True, db_comment='安装位置描述')
    brand_model = models.CharField(max_length=100, blank=True, null=True, db_comment='品牌型号')
    purchase_date = models.DateField(blank=True, null=True, db_comment='采购日期')
    warranty_end_date = models.DateField(blank=True, null=True, db_comment='保修期至')
    status = models.CharField(max_length=20, db_comment='当前状态(正常/故障/维修中/报废)')
    last_maintenance_date = models.DateField(blank=True, null=True, db_comment='上次维保日期')
    maintenance_cycle = models.IntegerField(blank=True, null=True, db_comment='维保周期(天)')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'device'
        db_table_comment = '设备表'

class Vehicle(models.Model):
    vehicle_id = models.BigAutoField(primary_key=True, db_comment='车辆ID')
    plate_number = models.CharField(unique=True, max_length=20, db_comment='车牌号')
    brand = models.CharField(max_length=50, blank=True, null=True, db_comment='品牌')
    model = models.CharField(max_length=50, blank=True, null=True, db_comment='型号')
    color = models.CharField(max_length=20, blank=True, null=True, db_comment='颜色')
    vehicle_type = models.CharField(max_length=20, db_comment='车辆类型(小轿车/SUV/货车)')
    user = models.ForeignKey(User, models.DO_NOTHING, blank=True, null=True, db_comment='所属用户ID')
    visitor = models.ForeignKey('users.Visitor', models.DO_NOTHING, blank=True, null=True, db_comment='所属访客ID')
    register_date = models.DateField(db_comment='注册日期')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'vehicle'
        db_table_comment = '车辆信息表'

